/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.Serializable;

import org.jefb.service.IUserSessionService;
import org.jefb.web.model.DirectoryMananagerModel;
import org.jefb.web.model.FileDataHolder;
import org.jefb.web.util.DateTimeUtils;
import org.jefb.web.util.Icons;
import org.jefb.web.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;

@org.springframework.stereotype.Component
@Scope("prototype")
public class FileInfoController extends GenericForwardComposer implements
		Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
	private DirectoryMananagerModel directoryManagerModel;
	@Autowired
	private IUserSessionService userSessionService;

	// UI-components
	protected Image fileIconImage;
	protected Label statusFilesizeLabel;
	protected Label fileNameLabel;
	protected Label modifiedLabel;

	public FileInfoController() {

	}

	public void init() {

	}

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		directoryManagerModel.addPropertyChangeListener(
				DirectoryMananagerModel.SELECTED_FILE_PROPERTY,
				new PropertyChangeListener() {
					public void propertyChange(PropertyChangeEvent arg0) {
						FileDataHolder selectedFileHolder = directoryManagerModel
								.getSelectedFile();
						if (selectedFileHolder != null) {
							File selectedFile = selectedFileHolder.getFile();
							if (selectedFile.isFile()) {
								fileIconImage.setSrc("../img/fs64/"
										+ Icons.getSrc(directoryManagerModel
												.getSelectedFile().getFile()
												.getName()));
							} else {
								if (selectedFile.canRead()) {
									fileIconImage
											.setSrc("../img/fs64/folder.png");
								} else {
									fileIconImage
											.setSrc("../img/fs64/folder_red2.png");
								}
							}
							statusFilesizeLabel.setValue(WebUtil
									.convertByteLength(selectedFile.length()));
							fileNameLabel.setValue(selectedFile.getName());
							modifiedLabel.setValue(DateTimeUtils
									.formatDate(selectedFile.lastModified())
									+ "\n"
									+ DateTimeUtils.formatTime(selectedFile
											.lastModified()));
						}
					}
				});
	}

	public void setDirectoryManagerModel(
			DirectoryMananagerModel directoryManagerModel) {
		this.directoryManagerModel = directoryManagerModel;
	}

	public DirectoryMananagerModel getDirectoryManagerModel() {
		return directoryManagerModel;
	}

	public void setUserSession(IUserSessionService userSessionService) {
		this.userSessionService = userSessionService;
	}

	public IUserSessionService getUserSession() {
		return userSessionService;
	}

}
